<?php
declare (strict_types=1);

namespace app\controller;

use think\exception\ValidateException;
use think\facade\Cookie;
use think\facade\Session;
use think\Request;
use app\model\Topic as TopicModel;

class Topic {
    protected $middleware = [
        'auth' => ['except' => ['save', 'delete']],

    ];
    public function save(Request $request) {
        try {
            validate([
                'content|博文内容' => 'require|max:2000',
            ])->batch()->check($request->post());
            $user = loginUser();
            $user->topics()->save(['content'=>$request->post('content')]);
            return redirect((string)url('home'))->with('success','博文发表成功~');
        } catch (ValidateException $e) {
            Session::flash('old_topic',$request->post('content'));
            if (!empty($errors = $e->getError())) {
                session('errors', $errors);
            }
            return redirect((string)url('home'));
        }
    }


    public function delete($id) {
        $topic = TopicModel::find($id);
        if($topic && isMineOpt($topic->user_id)){
            $topic->delete();
            return redirect((string)url('home'))->with('success','你成功删除了一篇博文~');
        }else{
            abort(404,'非法操作');
        }
    }
}
